package org.usp.poli.bipt.client.ui;


import java.util.Date;
import java.util.List;

import org.usp.poli.bipt.client.BrazilianIPT;
import org.usp.poli.bipt.client.GreetingService;
import org.usp.poli.bipt.client.GreetingServiceAsync;
import org.usp.poli.bipt.client.metadata.*;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;




public class Home {
	
	private final GreetingServiceAsync greetingService = GWT
	.create(GreetingService.class);
	
	private Button cadastroBtn;
	private MetaData metaData = new MetaData();
	private GeographicCoverage geographicCoverage = new GeographicCoverage();
	private ResourceOriginator resourceOriginator = new ResourceOriginator();
	private BasicMetadataTypes basicMetadataTypes = new BasicMetadataTypes();
	private GeneralKeywords generalKeywords = new GeneralKeywords();
	private IPRights ipRights = new IPRights();
	private Methods methods = new Methods();
	private ResearchProject researchProject = new ResearchProject();
	private TaxonomicCoverage taxonomicCoverage = new TaxonomicCoverage();
	private TemporalCoverage temporalCoverage = new TemporalCoverage();
	private LowestCommonTax lowestCommonTax = new LowestCommonTax();
	
	//private PushButton basicMetadataNextBtn = new PushButton();
	//...
	
	
	public Home() {
	}
	
	
	public Widget getHome(){
		
		final List<MetaData> metaData = null;
		final DecoratorPanel decPanel = new DecoratorPanel();
		final VerticalPanel vPanel = new VerticalPanel();
		
		final HTML novo = new HTML();
		novo.setHTML("<h1> Bem Vindo </h1> Esse é um projeto piloto que visa mapear as principais funcionalidades do IPT dentro das nuvens do Google. <br><br> Autores alunos André Quintela Bertuzzi, Bruno Vannucci e Paulo Pessoa da Escola Politécnica de engenharia. <br> <br> Coleções incluídas no Banco de Dados: <br>");
		
		BrazilianIPT.greetingService.getCollections(
				new AsyncCallback<List<MetaData>>() {
			public void onFailure(Throwable caught) {
				
			}
			
			
			public void onSuccess(List<MetaData> result) {
				
				VerticalPanel vPanel = new VerticalPanel();
				vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
				vPanel.add(novo);
				
				Grid grid = new Grid(result.size(), 4);
		        grid.setWidget(0, 0, new Label("Title"));
		        grid.setWidget(0, 1, new Label("Types"));
		        grid.setWidget(0, 2, new Label("Contact Name"));
		        grid.setWidget(0, 3, new Label("Description"));
		        grid.setStyleName("panelGrid");
		        
		        grid.getRowFormatter().setStyleName(0, "tableTitle");
		        for (final MetaData p: result) {
		        	int i=1;
		        	Label lblTitle = new Label(p.getTitle());
		        	lblTitle.setStyleName("labelLink");
		        	lblTitle.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							BrazilianIPT.callCollection(p.getKey());
							
						}
					});
		        	
		        	grid.setWidget(i, 0, lblTitle);
			        grid.setWidget(i, 1, new Label(p.getTypes()));
			        grid.setWidget(i, 2, new Label(p.getContactName()));
			        grid.setWidget(i, 3, new Label(p.getDescription()));
			        i++;
		        }
		        for (int i = 1; i < grid.getRowCount(); i=i+2) {
		            grid.getRowFormatter().setStyleName(i, "tableCell-even");
		        }
			    vPanel.add(grid);
		        decPanel.setWidget(vPanel);
			}
		});
		
		
		
	    
       
        
	    	    
	    return decPanel;
	}

	
}
